<%
# Copyright (C) 2011 - present Instructure, Inc.
#
# This file is part of Canvas.
#
# Canvas is free software: you can redistribute it and/or modify it under
# the terms of the GNU Affero General Public License as published by the Free
# Software Foundation, version 3 of the License.
#
# Canvas is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
# details.
#
# You should have received a copy of the GNU Affero General Public License along
# with this program. If not, see <http://www.gnu.org/licenses/>.
%>

<% add_crumb t(:sis_import_crumb, "SIS Import") %>
<% @active_tab = "sis_import" %>
<% content_for :page_title do %>{{#t}}SIS Import{{/t}}
<% end %>

<div>
  <h1>{{#t}}SIS Import{{/t}}</h1>
  <% if can_do(@account, @current_user, :import_sis) %>
    <div class="sis_messages" style="<%= hidden %>">
      <div class="ui-state-error sis_error_message" style="padding: 0.5em 1em"></div>
      <%= link_to t(:try_import_again_link, "Try import again"), account_sis_import_path %>
    </div>

    <div class="form" style="<%= hidden if @current_batch && @current_batch.importing? %>">
      <%= form_tag account_sis_imports_path(@account.id), :multipart => true, :id => "sis_importer" do %>
        <p class="instruction"><%= mt(:select_file_instructions,
                                      "Select the zip file that you want imported.  \n" +
                                          "For a description of how to generate these zip files, [please see this documentation](%{url}).",
                                      :url => "/doc/api/file.sis_csv.html") %></p>
        <div>
          <input type="file" name="attachment" style="font-size: 1.2em;"/>
          <p/>
          <label for="import_type"><%= before_label(t(:import_type_label, "Import type")) %></label>
          <select id="import_type" name="import_type">
            <% SisBatch.valid_import_types.each do |type, data| %>
              <option value="<%= type %>"
                      <% if data[:default] %> selected
                      <% end %>><%= data[:name].respond_to?(:call) ? data[:name].call : data[:name] %></option>
            <% end %>
          </select>

          <p/>
          <div id="batch_check">
            <input type="checkbox" name="batch_mode" value="1" id="batch_mode"/>
            <label for="batch_mode">{{#t}}This is a full batch update{{/t}}</label>
            <br>
            <p class='instruction'>
              <%= mt(:batch_import_instructions,
                     "If selected, this will delete everything for this term, which includes all courses and enrollments that are not in the selected import file above.  \nSee the documentation for details.") %>
            </p>
            <label id="batch_mode_term_id_label" for="batch_mode_term_id">
              {{#t}}Term:{{/t}}
            </label>
            <select id="batch_mode_term_id" name="batch_mode_term_id">
              <%= options_from_collection_for_select(@terms, :id, :name, @terms.first.try(:id)) %>
            </select>
          </div>

          <div id="stickiness_options">
            <div id="override_sis_stickiness_container">
              <input type="checkbox" name="override_sis_stickiness" value="1" id="override_sis_stickiness"/>
              <label for="override_sis_stickiness">{{#t}}Override UI changes{{/t}}</label>
              <p class='instruction'>
                <%= mt(:override_sis_stickiness_instructions,
                       "By default, UI changes have priority over SIS import changes; for a number of fields, the SIS import will not change that field's data if an admin has changed that field through the UI. If you select this option, this SIS import will override UI changes.  \nSee the documentation for details.") %>
              </p>
              <div id="add_sis_stickiness_container" style="padding-left: 20px;">
                <input type="checkbox" name="add_sis_stickiness" value="1" id="add_sis_stickiness"/>
                <label for="add_sis_stickiness">{{#t}}Process as UI changes{{/t}}</label>
                <p class='instruction'>
                  <%= mt(:add_sis_stickiness_instructions,
                         "With this option selected, changes made through this SIS import will be processed as if they are UI changes, preventing subsequent non-overriding SIS imports from changing the fields changed here.") %>
                </p>
              </div>
              <div id="clear_sis_stickiness_container" style="padding-left: 20px;">
                <input type="checkbox" name="clear_sis_stickiness" value="1" id="clear_sis_stickiness"/>
                <label for="clear_sis_stickiness">{{#t}}Clear UI-changed state{{/t}}</label>
                <p class='instruction'>
                  <%= mt(:clear_sis_stickiness_instructions,
                         "With this option selected, all fields in all records touched by this SIS import will be able to be changed in future non-overriding SIS imports.") %>
                </p>
              </div>
            </div>
          </div>
        </div>
        <div>
          <button type="submit" class="submit_button btn btn-primary">{{#t}}Process Data{{/t}}</button>
        </div>
        <div class="clear"></div>
      <% end %>
    </div>
  <% end %>

  <div class="progress_bar_holder" style="<%= hidden unless @current_batch && @current_batch.importing? %> margin-top: 10px;">
    <div class="copy_progress"></div>
    <div class="progress_message">{{#t}}The import process has started! This can take awhile if there is a lot to import. You can leave the page and come back.{{/t}}</div>
  </div>

  <div id="import_log_holder" style="<%= hidden %>">
    <h2>{{#t}}Import Progress Log{{/t}}</h2>
    <div id="import_log"></div>
  </div>

  <% pending_batch_count = @account.sis_batches.needs_processing.count %>
  <% pending_batch_count -= 1 if @current_batch && @current_batch.created? %>
  <% if pending_batch_count > 0 %>
    <div class="pending_batches" style="margin-top: 20px">
      <h2>{{#t}}Pending Batches{{/t}}</h2>
      <p><%= t(:pending_batches_count, {:one => "%{count} batch waiting to be processed.", :other => "%{count} batches waiting to be processed."}, :count => pending_batch_count) %></p>
    </div>
  <% end %>

  <% if show_last_batch %>
    <div class="last_batch" style="margin-top: 20px;">
      <h2>{{#t}}Last Batch{{/t}}</h2>
      <p><%= t(:started_at_message, "Started: %{started_at}", :started_at => datetime_string(@last_batch.created_at)) %>
        <br/>
        <% if @last_batch.diffing_data_set_identifier %>
        <p><%= t("Data Set Identifier: %{data_set_id}", data_set_id: @last_batch.diffing_data_set_identifier) %></p>
        <% if @last_batch.data[:diffed_against_sis_batch_id] %>
          <p>{{#t}}Incremental update successfully generated against a previous SIS Import.{{/t}}</p>
        <% end %>
      <% end %>
      <% if @last_batch.workflow_state == 'imported' %>
        {{#t}}All SIS data was successfully imported.{{/t}}
        <%= print_counts @last_batch %>
      <% elsif @last_batch.workflow_state == 'imported_with_messages' %>
        {{#t}}The SIS data was imported but with these messages:{{/t}}
        <% if @last_batch.errors_attachment_id %>
          <p>{{#t}}Download the complete list of errors and warnings here.{{/t}}
          <%= link_to "<i class='icon-download'></i>".html_safe,
                      file_download_url(@last_batch.errors_attachment,
                                        verifier: @last_batch.errors_attachment.uuid,
                                        download: '1',
                                        download_frd: '1'),
                      'aria-label' => t('Download errors_attchment'),
                      'title' => t('Download errors_attchment') %></p>
        <% end %>
        <%= print_messages(@last_batch) %>
        <%= print_counts @last_batch %>
      <% elsif @last_batch.workflow_state == 'failed' %>
        {{#t}}There was an error importing your SIS data. No records were imported.{{/t}}
      <% elsif @last_batch.workflow_state == 'failed_with_messages' %>
        {{#t}}The SIS data was imported but with these messages:{{/t}}
        <% if @last_batch.errors_attachment_id %>
          <p>{{#t}}Download the complete list of errors and warnings here.{{/t}}
            <%= link_to "<i class='icon-download'></i>".html_safe,
                        file_download_url(@last_batch.errors_attachment,
                                          verifier: @last_batch.errors_attachment.uuid,
                                          download: '1',
                                          download_frd: '1'),
                        'aria-label' => t('Download errors_attchment'),
                        'title' => t('Download errors_attchment') %></p>
        <% end %>
        {{#t}}No SIS records were imported. The import failed with these messages:{{/t}}
        <%= print_messages(@last_batch) %>
      <% elsif @last_batch.workflow_state == 'aborted' %>
        {{#t}}The previous SIS batch was aborted{{/t}}
        <%= print_messages(@last_batch) %>
      <% end %>
      </p>
    </div>
  <% end %>

</div>

<% js_bundle :sis_import %>
